<template>
  <div class="bigcontent">
    <div class="imgContent" v-if="upType=='img'">
      <span v-for="(img, index) in myImg" :key="index">
        <img :src="imgUrl + img" alt="" />
        <i
          class="el-icon-error circle"
          @click="delimg(index)"
          v-if="!islook"
        ></i>
      </span>
    </div>
    <div v-else>
      <div class="fileline" v-if="!islook">
        <div  class="filecon">
          <div >
            <el-button><i class="el-icon-upload"></i>选择上传文件</el-button>
          </div>
          <input
            class="filestyle"
            type="file"
            ref="fileInput"
            @change="upimg($event)"
          />
        </div>
        <slot name="fileslot"></slot>
      </div>
      <div class="filelist">
        <div class="item" v-for="(item, index) in myImg" :key="item.url">
          <div><i class="el-icon-document" style="margin-right: 5px;"></i>{{item.name}}</div>
          <i v-if="!islook" class="el-icon-error filedel" @click="delimg(index)"></i>
        </div>
      </div>
    </div>
    

    <div class="file" v-if="!islook && upType=='img'">
      <input
        type="file"
        ref="fileInput"
        accept="image/gif,image/jpeg,image/jpg,image/png,image/bmg"
        @change="upimg($event)"
      />
    </div>
  </div>
</template>

<script>
import { getQiniuToken } from "api/commonApi";
import config from "api/config";
import * as qiniu from "qiniu-js";
export default {
  props: {
    upType: {
      default: 'img' // file
    },
    imgs: {
      type: Array,
      default: () => []
    },
    islook: {
      type: Boolean,
      default: false
    },
    ismultiple: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    myImg() {
      return this.imgs;
    }
  },
  data() {
    return {
      imgUrl: config.imgUrl
    };
  },
  methods: {
    upimg(event) {
      var files = event.target.files;
      files = Array.from(files);
      files.forEach(file => {
        if (file.size > 5 * 1024 * 1024) {
          alert("上传文件只能为5M内");
        } else {
          getQiniuToken().then(res => {
            let token = res.rData[0].token;
            // file.name
            this._upImg(file, null, token);
          });
        }
      });
      this.$nextTick(() => {
        event.target.value = "";
      });
    },
    _upImg(file, key, token) {
      let fileName = file.name
      let putExtra = {
        fname: null,
        params: {},
        mimeType: null
      };
      let config = {
        useCdnDomain: true,
        region: null
      };
      var observable = qiniu.upload(file, key, token, putExtra, config);
      observable.subscribe({
        next: res => {
          console.log("进度", res);
        },
        error: err => {
          console.log("错误", err);
        },
        complete: res => {
          console.log("完成", res);
          let key = res.key
          if(this.upType=='file') {
            key = {
              name: fileName,
              url: res.key
            }
          }
          if (this.ismultiple) {
            console.log(key)
            this.$emit("update:imgs", [...this.myImg, key]);
          } else {
            this.$emit("update:imgs", [key]);
          }
        }
      });
    },
    delimg(index) {
      this.myImg.splice(index, 1);
      this.$emit("update:imgs", [...this.myImg]);
    }
  },
  mounted() {
    if (this.ismultiple && !this.islook) {
      this.$refs.fileInput.setAttribute("multiple", "multiple");
    }
  }
};
</script>
<style lang="scss" scoped>
.bigcontent {
  display: flex;
}
.fileline{
  display: flex;
}
.filelist{
  .item{
    width: 300px;
    display: flex;
    height: 35px;
    justify-content: space-between;
    align-items: center;
  }
  .filedel{
    cursor: pointer;
  }
}
.filecon {
  position: relative;
  margin-right: 10px;
  .filestyle{
    position: absolute;
    height: 100%;
    width: 100%;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
  }
}
.imgContent {
  display: flex;
  span {
    position: relative;
    height: 100px;
    width: 100px;
    margin-right: 5px;
    img {
      height: 100px;
      width: 100px;
    }
    .circle {
      position: absolute;
      opacity: 1;
      right: -5px;
      top: -5px;
      display: flex;
      line-height: 14px;
      width: 18px;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        color: red;
        transform: rotate(180deg);
        transform-origin: center;
      }
    }
  }
}
.file {
  position: relative;
  display: inline-block;
  background: url("./rect.png") center no-repeat;
  height: 100px;
  width: 100px;
  input {
    position: absolute;
    height: 100%;
    width: 100%;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
  }
}
</style>
